// pages/workOrder/workOrder.js
const app = getApp();
const {
  wordOrder,
  acceptOrder,
  myInfo
} = require('../../config.js');
let timer;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    index: 1,
    dataList0: [],
    pageBean0: {},
    dataList1: [],
    pageBean1: {},
    dataList2: [],
    pageBean2: {},
    dataList3: [],
    pageBean3: {},
    lesseeNameVague: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { type } = options;
    this.setData({
      index: type
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      dataList0: [],
      dataList1: [],
      dataList2: [],
      dataList3: []
    })
    wx.showNavigationBarLoading();
    app.appRequest({
      url: wordOrder,
      data: {
        lesseeNameVague: this.data.lesseeNameVague
      },
      method: 'POST',
      success: (res) => {
        wx.hideNavigationBarLoading();
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList0: dataList,
          pageBean0: pageBean,
        })
      }
    })
    app.appRequest({
      url: wordOrder,
      method: 'POST',
      data: {
        lesseeNameVague: this.data.lesseeNameVague,
        status: 1
      },
      success: (res) => {
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList1: dataList,
          pageBean1: pageBean,
        })
      }
    })
    app.appRequest({
      url: wordOrder,
      method: 'POST',
      data: {
        lesseeNameVague: this.data.lesseeNameVague,
        status: 2
      },
      success: (res) => {
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList2: dataList,
          pageBean2: pageBean,
        })
      }
    })
    app.appRequest({
      url: wordOrder,
      method: 'POST',
      data: {
        lesseeNameVague: this.data.lesseeNameVague,
        status: 3
      },
      success: (res) => {
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList3: dataList,
          pageBean3: pageBean,
        })
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    app.appRequest({
      url: wordOrder,
      data: {
        lesseeNameVague: this.data.lesseeNameVague
      },
      method: 'POST',
      success: (res) => {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList0: dataList,
          pageBean0: pageBean,
        })
      }
    })
    app.appRequest({
      url: wordOrder,
      method: 'POST',
      data: {
        lesseeNameVague: this.data.lesseeNameVague,
        status: 1
      },
      success: (res) => {
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList1: dataList,
          pageBean1: pageBean,
        })
      }
    })
    app.appRequest({
      url: wordOrder,
      method: 'POST',
      data: {
        lesseeNameVague: this.data.lesseeNameVague,
        status: 2,
      },
      success: (res) => {
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList2: dataList,
          pageBean2: pageBean,
        })
      }
    })
    app.appRequest({
      url: wordOrder,
      method: 'POST',
      data: {
        lesseeNameVague: this.data.lesseeNameVague,
        status: 3,
      },
      success: (res) => {
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList3: dataList,
          pageBean3: pageBean,
        })
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  changeIndex: function (e) {
    let index = e.target.dataset.index;
    this.setData({
      index: index
    })
  },
  transferOrder: function (e) {
    console.log(e);
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/transferOrder/transferOrder?id=${id}`,
    })
  },
  acceptOrder: function (e) {
    let that = this;
    let id = e.currentTarget.dataset.id;
    let number = e.currentTarget.dataset.number;
    let name = e.currentTarget.dataset.name;
    wx.showModal({
      title: `接收${name}的订单?`,
      content: `订单编号: ${number}`,
      confirmColor: '#1E66CC',
      success(res) {
        if (res.confirm) {
          app.appRequest({
            url: acceptOrder,
            method: 'POST',
            data: {
              id
            },
            success: (res) => {
              if (res.code == 0) {
                wx.showToast({
                  title: '接单成功',
                  icon: 'none'
                })
                //刷新页面数据
                wx.showNavigationBarLoading();
                app.appRequest({
                  url: wordOrder,
                  data: {
                    lesseeNameVague: that.data.lesseeNameVague
                  },
                  method: 'POST',
                  success: (res) => {
                    wx.hideNavigationBarLoading();
                    let user = res.data.user;
                    let pageBean = res.data.pageBean;
                    let dataList = res.data.pageBean.result.map((item) => {
                      if (user[item.userId]) {
                        item.address = user[item.userId].address;
                        item.name = user[item.userId].name;
                        item.name = user[item.userId].name;
                      }
                      return item
                    })
                    that.setData({
                      dataList0: dataList,
                      pageBean0: pageBean,
                      index: 3
                    })
                  }
                })
                app.appRequest({
                  url: wordOrder,
                  method: 'POST',
                  data: {
                    lesseeNameVague: that.data.lesseeNameVague,
                    status: 1
                  },
                  success: (res) => {
                    let user = res.data.user;
                    let pageBean = res.data.pageBean;
                    let dataList = res.data.pageBean.result.map((item) => {
                      if (user[item.userId]) {
                        item.address = user[item.userId].address;
                        item.name = user[item.userId].name;
                        item.name = user[item.userId].name;
                      }
                      return item
                    })
                    that.setData({
                      dataList1: dataList,
                      pageBean1: pageBean,
                    })
                  }
                })
                app.appRequest({
                  url: wordOrder,
                  method: 'POST',
                  data: {
                    lesseeNameVague: that.data.lesseeNameVague,
                    status: 2,
                  },
                  success: (res) => {
                    let user = res.data.user;
                    let pageBean = res.data.pageBean;
                    let dataList = res.data.pageBean.result.map((item) => {
                      if (user[item.userId]) {
                        item.address = user[item.userId].address;
                        item.name = user[item.userId].name;
                        item.name = user[item.userId].name;
                      }
                      return item
                    })
                    that.setData({
                      dataList2: dataList,
                      pageBean2: pageBean,
                    })
                  }
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  navigatorToOrderDetail: function (e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/orderDetail/orderDetail?id=${id}`,
    })
  },
  workOrderChange: function (e) {
    let lesseeNameVague = e.detail.value
    this.setData({
      lesseeNameVague
    })
    clearTimeout(timer);
    timer = setTimeout(() => {
      wx.showLoading({
        title: '加载中...',
      })
      app.appRequest({
        url: wordOrder,
        data: {
          lesseeNameVague
        },
        method: 'POST',
        success: (res) => {
          console.log(res)
          let user = res.data.user;
          let pageBean = res.data.pageBean;
          let dataList = res.data.pageBean.result.map((item) => {
            if (user[item.userId]) {
              item.address = user[item.userId].address;
              item.name = user[item.userId].name;
              item.name = user[item.userId].name;
            }
            return item
          })
          this.setData({
            dataList0: dataList,
            pageBean0: pageBean
          })
        }
      })
      app.appRequest({
        url: wordOrder,
        data: {
          lesseeNameVague,
          status: 1
        },
        method: 'POST',
        success: (res) => {
          let user = res.data.user;
          let pageBean = res.data.pageBean;
          let dataList = res.data.pageBean.result.map((item) => {
            if (user[item.userId]) {
              item.address = user[item.userId].address;
              item.name = user[item.userId].name;
              item.name = user[item.userId].name;
            }
            return item
          })
          this.setData({
            dataList1: dataList,
            pageBean1: pageBean
          })
        }
      })
      app.appRequest({
        url: wordOrder,
        data: {
          lesseeNameVague: this.data.lesseeNameVague,
          status: 2
        },
        method: 'POST',
        success: (res) => {
          let user = res.data.user;
          let pageBean = res.data.pageBean;
          let dataList = res.data.pageBean.result.map((item) => {
            if (user[item.userId]) {
              item.address = user[item.userId].address;
              item.name = user[item.userId].name;
              item.name = user[item.userId].name;
            }
            return item
          })
          this.setData({
            dataList2: dataList,
            pageBean2: pageBean
          })
        }
      })
      app.appRequest({
        url: wordOrder,
        data: {
          lesseeNameVague: this.data.lesseeNameVague,
          status: 3
        },
        method: 'POST',
        success: (res) => {
          let user = res.data.user;
          let pageBean = res.data.pageBean;
          let dataList = res.data.pageBean.result.map((item) => {
            if (user[item.userId]) {
              item.address = user[item.userId].address;
              item.name = user[item.userId].name;
              item.name = user[item.userId].name;
            }
            return item
          })
          this.setData({
            dataList3: dataList,
            pageBean3: pageBean
          })
        }
      })
      wx.hideLoading()
    }, 1000)
  },
  lower: function () {
    let {
      index,
      pageBean0,
      pageBean1,
      pageBean2,
      pageBean3
    } = this.data;
    if (index == 1) {
      let dataList0 = [...this.data.dataList0];
      if (pageBean0.hasNextPage) {
        app.appRequest({
          url: wordOrder,
          method: 'POST',
          data: {
            page: pageBean0.curPage + 1,
            pageSize: pageBean0.pageSize,
          },
          success: (res) => {
            let user = res.data.user;
            let pageBean = res.data.pageBean;
            let dataList = res.data.pageBean.result.map((item) => {
              if (user[item.userId]) {
                item.address = user[item.userId].address;
                item.name = user[item.userId].name;
                item.name = user[item.userId].name;
              }
              return item
            })
            this.setData({
              dataList0: [...dataList0, ...dataList],
              pageBean0: pageBean,
            });
          }
        })
      }
    } else if (index == 2) {
      let dataList1 = [...this.data.dataList1];
      if (pageBean1.hasNextPage) {
        app.appRequest({
          url: wordOrder,
          method: 'POST',
          data: {
            status: 1,
            page: pageBean1.curPage + 1,
            pageSize: pageBean1.pageSize,
          },
          success: (res) => {
            let user = res.data.user;
            let pageBean = res.data.pageBean;
            let dataList = res.data.pageBean.result.map((item) => {
              if (user[item.userId]) {
                item.address = user[item.userId].address;
                item.name = user[item.userId].name;
                item.name = user[item.userId].name;
              }
              return item
            })
            this.setData({
              dataList1: [...dataList1, ...dataList],
              pageBean1: pageBean,
            })
          }
        })
      }
    } else if (index == 2) {
      let dataList2 = [...this.data.dataList2];
      if (pageBean2.hasNextPage) {
        app.appRequest({
          url: wordOrder,
          method: 'POST',
          data: {
            status: 3,
            page: pageBean2.curPage + 1,
            pageSize: pageBean2.pageSize,
          },
          success: (res) => {
            let user = res.data.user;
            let pageBean = res.data.pageBean;
            let dataList = res.data.pageBean.result.map((item) => {
              if (user[item.userId]) {
                item.address = user[item.userId].address;
                item.name = user[item.userId].name;
                item.name = user[item.userId].name;
              }
              return item
            })
            this.setData({
              dataList2: [...dataList2, ...dataList],
              pageBean2: pageBean,
            })
          }
        })
      }
    } else if (index == 3) {
      let dataList3 = [...this.data.dataList3];
      if (pageBean3.hasNextPage) {
        app.appRequest({
          url: wordOrder,
          method: 'POST',
          data: {
            status: 3,
            page: pageBean3.curPage + 1,
            pageSize: pageBean3.pageSize,
          },
          success: (res) => {
            let user = res.data.user;
            let pageBean = res.data.pageBean;
            let dataList = res.data.pageBean.result.map((item) => {
              if (user[item.userId]) {
                item.address = user[item.userId].address;
                item.name = user[item.userId].name;
                item.name = user[item.userId].name;
              }
              return item
            })
            this.setData({
              dataList3: [...dataList3, ...dataList],
              pageBean3: pageBean,
            })
          }
        })
      }
    }
  },
  upper: function () {
    wx.showNavigationBarLoading();
    wx.startPullDownRefresh();
    app.appRequest({
      url: wordOrder,
      data: {
        lesseeNameVague: this.data.lesseeNameVague
      },
      method: 'POST',
      success: (res) => {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh()
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList0: dataList,
          pageBean0: pageBean,
        })
      }
    })
    app.appRequest({
      url: wordOrder,
      method: 'POST',
      data: {
        lesseeNameVague: this.data.lesseeNameVague,
        status: 1
      },
      success: (res) => {
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList1: dataList,
          pageBean1: pageBean,
        })
      }
    })
    app.appRequest({
      url: wordOrder,
      method: 'POST',
      data: {
        lesseeNameVague: this.data.lesseeNameVague,
        status: 2,
      },
      success: (res) => {
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList2: dataList,
          pageBean2: pageBean,
        })
      }
    })
    app.appRequest({
      url: wordOrder,
      method: 'POST',
      data: {
        lesseeNameVague: this.data.lesseeNameVague,
        status: 3,
      },
      success: (res) => {
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList3: dataList,
          pageBean3: pageBean,
        })
      }
    })
  },
  clear: function () {
    this.setData({
      lesseeNameVague: ''
    })
    app.appRequest({
      url: wordOrder,
      method: 'POST',
      success: (res) => {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh()
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList0: dataList,
          pageBean0: pageBean,
        })
      }
    })
    app.appRequest({
      url: wordOrder,
      method: 'POST',
      data: {
        status: 1
      },
      success: (res) => {
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList1: dataList,
          pageBean1: pageBean,
        })
      }
    })
    app.appRequest({
      url: wordOrder,
      method: 'POST',
      data: {
        status: 2,
      },
      success: (res) => {
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList2: dataList,
          pageBean2: pageBean,
        })
      }
    })
    app.appRequest({
      url: wordOrder,
      method: 'POST',
      data: {
        status: 3,
      },
      success: (res) => {
        let user = res.data.user;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (user[item.userId]) {
            item.address = user[item.userId].address;
            item.name = user[item.userId].name;
            item.name = user[item.userId].name;
          }
          return item
        })
        this.setData({
          dataList3: dataList,
          pageBean3: pageBean,
        })
      }
    })
  }
})